home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Reference Guide
/
C-C++ Interactive Reference Guide.iso
/
c_ref
/
csource5
/
376_01
/
os2tool.000
/
CUTHUGE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-29
|
8KB
|
315 lines
/*
* CUTHUGE.C - Cut a huge file into smaller more manageable pieces.
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 24.8.1992
*/
static char *VERSION = "Version 1.0, Copyright(c) Martti Ylikoski, 1992" ;
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <io.h>
#include <ctype.h>
#include <errno.h>
#include <param.h>
#include <paramstd.h>
#define INCL_VIO
#include <os2.h>
static char *progname ;
extern unsigned long pflags ;
ParamEntry pentry[12] = {
"P", &ParamSetPause, 0,
"F", &ParamSetFold, 0,
"V", &ParamSetVerbose, 0,
"R", &ParamSetReport, 0,
"S", &ParamSetSubDirs, 0,
"?", &ParamSetHelp, 1,
"H", &ParamSetHelp, 1,
"NOD", &ParamSetNoDefault, 0,
"TEST", &ParamSetTest, 0,
"Y", &ParamSetYes, 0,
"N", &ParamSetTest, 0,
"\0", NULL, 0
} ;
ParamBlock params = {
"/-", IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
pentry
} ;
static int fhnd, writecnt = 1 ;
static int cols, count, stdinassumed ;
#define MAXBUFLEN 512
/* local prototypes */
static int cuttext (char *fname, char *base, int clines) ;
FILE *newbase( char *basename, int *nameext, char *outmode) ;
static int cutbin (char *cutfile, char *basename, int cutsize) ;
int main(int argc, char *argv[])
{
int i, nargc, binsize, binmode ;
char *base ;
base = "PART" ;
progname = argv[0] ;
count = 1000 ; /* default for lines to include */
binsize = 32 * 1024 ;
stdinassumed = FALSE ;
binmode = FALSE ;
ParamHandle(¶ms, &argc, argv) ;
if (pflags & PA_HELP)
{
fputs("CUTHUGE - cut a huge file into smaller more manageable pieces.\n", stderr) ;
fputs(VERSION, stderr) ;
fputs("\nUsage: cuthuge [file] [-lines] [/BIN] [/B basename] [/H | /?]\n", stderr) ;
fputs("Where:\n/B = give new basename (default PART)\n", stderr) ;
fputs("-lines = how many lines to include in each file or the size of each part in bytes in /BIN mode\n/H,/? = Show this help.\n", stderr) ;
return( 0 ) ;
}
fputs("CUTHUGE - cut a huge file into smaller more manageable pieces.\n", stderr) ;
fputs(VERSION, stderr) ;
nargc = argc ;
for (i = 1 ; i < argc ; i++)
if (argv[i][0] == '-' || argv[i][0] == '/')
{
if (strlen(argv[i]) == 1)
{
stdinassumed = TRUE ;
argv[i] = NULL ;
nargc -- ;
continue ;
}
else
if ( isdigit(argv[i][1]) != 0)
{
count = atoi(&argv[i][1]) ;
argv[i] = NULL ;
nargc -- ;
}
else
if (strcmpi("bin", &argv[i][1]) == 0)
{
binmode = TRUE ;
argv[i] = NULL ;
nargc -- ;
}
else
if (strcmpi("B", &argv[i][1]) == 0)
{
if (argc >= i+1)
{
fprintf(stderr, "%s: no base name given with /B\nExiting...\n", progname) ;
return( 1 ) ;
}
if (strlen(argv[i+1]) != 4)
{
fprintf(stderr, "%s: base name must be exatly 4 characters long\nExiting...\b", progname) ;
return( 1 ) ;
}
base = argv[i+1] ;
argv[i] = NULL ;
nargc -- ;
argv[i+1] = NULL ;
nargc -- ;
i++ ;
}
}
if (binmode == TRUE && count < 1024 )
{
fputs("Minumum buffer size for /BIN-mode is 1024\nExiting...\n", stderr) ;
return( 1 ) ;
}
if (nargc == 1)
{
fputs("\nStdin assumed\n", stderr) ;
if (binmode == FALSE)
cuttext("-", base, count) ;
else
cutbin("-", base, count) ;
}
else
{
for (i = 1 ; i < argc ; i++)
if (argv[i] != NULL)
if (binmode == FALSE)
cuttext(argv[i], base, count) ;
else
cutbin(argv[i], base, count) ;
if (stdinassumed == TRUE)
if (binmode == FALSE)
cuttext("-", base, count) ;
else
cutbin("-", base, count) ;
}
return( 0 ) ;
}
/*****************************************
*
* cuttext - cut a huge text file into smaller pieces.
*
******************************************/
static int cuttext (char *cutfile, char *basename, int cutlines)
{
FILE *cutfptr, *outfptr ;
char buf[MAXBUFLEN], *line ;
int lineswritten, nameext ;
lineswritten = 0 ;
nameext = 0 ;
/* open file */
if (strcmp(cutfile, "-") == 0)
cutfptr = stdin ;
else
if ((cutfptr = fopen(cutfile, "r")) == NULL)
{
printf("%s: unable to open file %s for reading...\n", progname, cutfile) ;
printf("Exiting...\n") ;
return( 1 ) ;
}
if (( outfptr = newbase(basename, &nameext, "w")) == NULL)
{
fputs("Error opening new parts file...",stderr) ;
fputs("CUTHUGE terminated...", stderr) ;
fputs("Possibly temporary files left on disk", stderr) ;
fclose(cutfptr) ;
return( 1 ) ;
}
while (( line = fgets(buf, sizeof(buf), cutfptr)) != NULL)
{
fputs(buf, outfptr) ;
lineswritten++ ;
if (lineswritten >= cutlines)
{
fclose (outfptr) ;
lineswritten = 0 ;
if (( outfptr = newbase(basename, &nameext,"w")) == NULL)
{
fputs("Error opening new parts file...", stderr) ;
fputs("CUTHUGE terminated...", stderr) ;
fputs("Possibly temporary files left on disk", stderr) ;
fclose(cutfptr) ;
return( 1 ) ;
}
}
}
/* close file */
fclose ( cutfptr ) ;
}
FILE *newbase( char *basename, int *nameext, char *outmode)
{
char namebuf[100] ;
int i ;
FILE *fptr ;
for( i = *nameext; i <INT_MAX; i++)
{
sprintf(namebuf,"%4.4s%4.4d.CUT",basename, i) ;
if (access(namebuf, 00) != 0) /* does not exist */
break ;
}
if (i == INT_MAX)
return ( NULL ) ;
if ((fptr = fopen(namebuf, outmode)) == NULL)
{
return( NULL ) ;
}
return( fptr ) ;
}
/*****************************************
*
* cutbin - cut a huge text file into smaller pieces (binary mode).
*
******************************************/
static int cutbin (char *cutfile, char *basename, int cutsize)
{
FILE *cutfptr, *outfptr ;
char buf[1024] ;
int nameext, overflow, rounds, totalrounds, readcnt ;
rounds = 0 ;
nameext = 0 ;
overflow = cutsize % sizeof(buf) ;
totalrounds = cutsize / sizeof(buf) ;
/* open file */
if (strcmp(cutfile, "-") == 0)
cutfptr = stdin ;
else
if ((cutfptr = fopen(cutfile, "rb")) == NULL)
{
printf("%s: unable to open file %s for reading...\n", progname, cutfile) ;
printf("Exiting...\n") ;
return( 1 ) ;
}
if (( outfptr = newbase(basename, &nameext, "wb")) == NULL)
{
fputs("Error opening new parts file...",stderr) ;
fputs("CUTHUGE terminated...", stderr) ;
fputs("Possibly temporary files left on disk", stderr) ;
fclose(cutfptr) ;
return( 1 ) ;
}
while ( (readcnt = read( fileno(cutfptr), buf, sizeof(buf) )) )
{
fwrite(buf, readcnt, (size_t) 1, outfptr) ;
rounds ++ ;
if (feof(cutfptr))
break ;
if (rounds >= totalrounds)
{
readcnt = read(fileno(cutfptr), buf, overflow) ;
fwrite(buf, readcnt, (size_t) 1, outfptr) ;
fclose (outfptr) ;
rounds = 0 ;
if (feof(cutfptr))
break ;
if (( outfptr = newbase(basename, &nameext, "wb")) == NULL)
{
fputs("Error opening new parts file...", stderr) ;
fputs("CUTHUGE terminated...", stderr) ;
fputs("Possibly temporary files left on disk", stderr) ;
fclose(cutfptr) ;
return( 1 ) ;
}
}
}
/* close file */
fclose ( cutfptr ) ;
}